/*
 * Copyright (c) 2011 Imre Fazekas.
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the Brillien nor the names of its
 * terms and concepts may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.vii.streamline.json;

import com.vii.streamline.services.error.StreamLineException;
import com.vii.streamline.services.json.JsonServices;

import java.util.List;

/**
 * Specialized JSON-based services managing all JSON-RPC-related activities
 */
public class JsonRpcServices {

    public static final String JSON_RPC_VERSION = "2.0";

    public static final JsonRpcError ERROR_PARSE;
    public static final JsonRpcError ERROR_INVALID_REQUEST;
    public static final JsonRpcError ERROR_METHOD_NOT_FOUND;
    public static final JsonRpcError ERROR_INVALID_PARAMS;
    public static final JsonRpcError ERROR_INTERNAL_ERROR;
    public static final JsonRpcError ERROR_INVALID_CREDENTIALS;
    public static final JsonRpcError ERROR_INSUFFICIENT_CREDENTIALS;
    public static final JsonRpcError ERROR_INVALID_REFERENCES;
    public static final JsonRpcError ERROR_INVALID_SESSION;
    public static final JsonRpcError ERROR_MISSING_DATA;
    public static final JsonRpcError ERROR_BROKEN_EXTERNAL;
    public static final JsonRpcError ERROR_ILLEGAL_INNER_STATE;
    public static final JsonRpcError ERROR_SECURITY_VIOLATION;
    public static final JsonRpcError ERROR_CONNECTION_BROKEN;
    public static final JsonRpcError ERROR_SERVER_ERROR;

    static{
        ERROR_PARSE = new JsonRpcError( StreamLineException.ERROR_PARSE, "Parse error", null );
        ERROR_INVALID_REQUEST = new JsonRpcError( StreamLineException.ERROR_INVALID_REQUEST, "Invalid Request", null );
        ERROR_METHOD_NOT_FOUND = new JsonRpcError( StreamLineException.ERROR_METHOD_NOT_FOUND, "Method not found", null );
        ERROR_INVALID_PARAMS = new JsonRpcError( StreamLineException.ERROR_INVALID_PARAMS, "Invalid params", null );
        ERROR_INTERNAL_ERROR = new JsonRpcError( StreamLineException.ERROR_INTERNAL_ERROR, "Internal error", null );

        // Free from -32000 to -32099
        ERROR_INVALID_CREDENTIALS = new JsonRpcError( StreamLineException.ERROR_INVALID_CREDENTIALS, "Invalid credentials", null );
        ERROR_INSUFFICIENT_CREDENTIALS = new JsonRpcError( StreamLineException.ERROR_INSUFFICIENT_CREDENTIALS, "Insufficient credentials", null );
        ERROR_INVALID_REFERENCES = new JsonRpcError( StreamLineException.ERROR_INVALID_REFERENCES, "References are invalid", null );
        ERROR_INVALID_SESSION = new JsonRpcError( StreamLineException.ERROR_INVALID_SESSION, "Session is invalid", null );
        ERROR_MISSING_DATA = new JsonRpcError( StreamLineException.ERROR_MISSING_DATA, "A data is missing", null );
        ERROR_BROKEN_EXTERNAL = new JsonRpcError( StreamLineException.ERROR_BROKEN_EXTERNAL, "An external service is broken", null );
        ERROR_ILLEGAL_INNER_STATE = new JsonRpcError( StreamLineException.ERROR_ILLEGAL_INNER_STATE, "Illegal Inner State", null );
        ERROR_SECURITY_VIOLATION = new JsonRpcError( StreamLineException.ERROR_SECURITY_VIOLATION, "Security violation", null );
        ERROR_CONNECTION_BROKEN = new JsonRpcError( StreamLineException.ERROR_CONNECTION_BROKEN, "Connection is broken.", null );
        ERROR_SERVER_ERROR = new JsonRpcError( -StreamLineException.ERROR_SERVER_ERROR, "Server error", null );
    }

    public static JsonRpcResponse getSimpleJsonRpcResponse( String content ) throws StreamLineException {
        try{
            JsonRpcResponse response = JsonServices.parseJSON( content, JsonRpcResponse.class );
            return response;
        } catch( Exception e ){
            e.printStackTrace();
            throw new StreamLineException( ERROR_INVALID_REQUEST.getCode(), ERROR_INVALID_REQUEST.getMessage() );
        }
    }

    public static JsonRpcRequest getSimpleJsonRpcRequest( String content ) throws StreamLineException {
        try{
            JsonRpcRequest request = JsonServices.<JsonRpcRequest>parseJSON( content, JsonRpcRequest.class );
            return request;
        } catch( Exception e ){
            e.printStackTrace();
            throw new StreamLineException( ERROR_INVALID_REQUEST.getCode(), ERROR_INVALID_REQUEST.getMessage() );
        }
    }

    public static List<JsonRpcResponse> getBatchJsonRpcResponse( String content ) throws StreamLineException {
        try{
            List<JsonRpcResponse> responses = JsonServices.parseJSON( content, "[]", JsonRpcResponse.class );

            return responses;
        } catch( Exception e ){
            throw new StreamLineException( ERROR_INVALID_REQUEST.getCode(), ERROR_INVALID_REQUEST.getMessage() );
        }
    }

    public static List<JsonRpcRequest> getBatchJsonRpcRequests( String content ) throws StreamLineException {
        try{
            List<JsonRpcRequest> requests = JsonServices.parseJSON( content, "[]", JsonRpcRequest.class );

            return requests;
        } catch( Exception e ){
            throw new StreamLineException( ERROR_INVALID_REQUEST.getCode(), ERROR_INVALID_REQUEST.getMessage() );
        }
    }

}
